//
//  BHCoachInfoHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/2.
//

import UIKit

class BHCoachInfoHeaderView: UIView {

    @IBOutlet weak var place_imageView: UIImageView!
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var buyCount_label: UILabel!
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 8
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    static func loadFromXib() -> BHCoachInfoHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHCoachInfoHeaderView", owner: nil, options: nil)
        let view: BHCoachInfoHeaderView = viewArray![0] as! BHCoachInfoHeaderView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.bottom()?.equalTo()(self.place_imageView)
        }
    }
    
    // 教练详情
    var model: BHCoachDetailModel? {
        didSet {
            if model == nil {
                return
            }
            self.name_label.text = model?.name
            self.buyCount_label.text = "课程已被购买\(model!.buyNumber)次"
            if model!.images.count != 0 {
                self.bannerView.sourceArray = model!.images
            }
        }
    }
    
    // 商品详情
    var proDetailModel: BHGoodDetailModel? {
        didSet {
            if proDetailModel == nil {
                return
            }
            self.name_label.text = proDetailModel?.name
            if proDetailModel!.purchaseNumber == -1 {
                self.buyCount_label.text = "已购买0次"
            }else {
                self.buyCount_label.text = "已购买\(proDetailModel!.purchaseNumber)次"
            }
            if proDetailModel!.cover.isEmpty == false {
                self.bannerView.sourceArray = proDetailModel?.images
            }
        }
    }
    
    
    
    static var viewHeight: CGFloat {
        get {
            return CGFloat(210)+80
        }
    }
}
extension BHCoachInfoHeaderView: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        if self.model != nil {
            let url = self.model!.images[indexPath.item%self.model!.images.count]
            cell.bannerImageUrl = url
        }
        if self.proDetailModel != nil {
            let url = self.proDetailModel!.images[indexPath.item%self.proDetailModel!.images.count]
            cell.bannerImageUrl = url.imageUrl ?? ""
        }
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width, height: CGFloat(210).radius)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        if self.model != nil {
            return self.model!.images.count
        }
        if self.proDetailModel != nil {
            return 1
        }
        return 0
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {
        if self.proDetailModel != nil {
            BHMediaTool.showPreview(array: self.proDetailModel!.images, current: index)
        }
    }
}
